﻿using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.Linq;

namespace PKU
{
    [Activity(Label = "北大网关", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        数据.用户信息[] 学生信息 = new 数据.用户信息[2];

        网络 web = new 网络();
        //各种按钮
        RadioButton radioButton, radioButton1;
        TextView textBlock;
        Button button1, button2, button3;
        Spinner 学号;
        
        protected override void OnCreate(Bundle bundle)
        {
            //RequestWindowFeature(WindowFeatures.NoTitle);//还是需要操作栏的, 要菜单按钮
            base.OnCreate(bundle);
            设置初始页面();
            
        }
        private void 设置初始页面()
        {
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
            //增加用户信息列表
            学生信息[0] = new 数据.用户信息 { 学号 = "1301110110", 密码 = "Oudanyi6456" };
            学生信息[1] = new 数据.用户信息 { 学号 = "gcuspea", 密码 = "phyfsc508" };

            // Get our button from the layout resource,
            // and attach an event to it
            radioButton = FindViewById<RadioButton>(Resource.Id.radioButton1);
            radioButton1 = FindViewById<RadioButton>(Resource.Id.radioButton2);
            textBlock = FindViewById<TextView>(Resource.Id.信息文本);
            button1 = FindViewById<Button>(Resource.Id.button1);
            button2 = FindViewById<Button>(Resource.Id.button2);
            button3 = FindViewById<Button>(Resource.Id.button3);
            学号 = (Spinner)FindViewById(Resource.Id.学生信息);

            ArrayAdapter<string> adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleSpinnerItem);
            adapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
            for (int i = 0; i < 学生信息.Length; i++) adapter.Add(学生信息[i].学号);
            学号.Adapter = adapter;

            button1.Click += 连接;
            button2.Click += 连接;
            button3.Click += 连接;
            
        }
        /// <summary>
        /// 设置选项菜单(ActionBarMenu)
        /// </summary>
        /// <param name="menu"></param>
        /// <returns></returns>
        public override bool OnCreateOptionsMenu(IMenu menu)
        {
            MenuInflater.Inflate(Resource.Menu.ActionBarMenu, menu);
            return true;
            //var shareMenuItem = menu.FindItem(Resource.Id.shareMenuItem);
            //var shareActionProvider =
            //   (ShareActionProvider)shareMenuItem.ActionProvider;
            //shareActionProvider.SetShareIntent(CreateIntent());
        }
        public override bool OnOptionsItemSelected(IMenuItem item)
        {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            switch (item.ItemId)
            {
                case Resource.Id.学号信息://明文存储你的用户名和密码

                    return true;
                case Resource.Id.提交位置:
                    LayoutInflater inflater = LayoutInflater;
                    builder.SetView(inflater.Inflate(Resource.Layout.Submit,null))
                        .SetTitle("提交位置")
                        .SetPositiveButton("确定", (sender, e) => { })
                        .SetNegativeButton("取消",(sender,e)=> { });
                    builder.Create().Show();
                    return true;
                case Resource.Id.关于:
                    builder.SetMessage("作者:欧伟科\n联系方式:Brainor@qq.com")
                        .SetTitle("关于")
                        .SetPositiveButton("OK", (sender, e) => { });
                    builder.Create().Show();
                    return true;
                default:

                    return true;
            }
        }

        static string[] 连接类型 = new string[] { "ipgwopen", "ipgwopenall", "ipgwclose", "ipgwcloseall" };//连接, 收费链接, 断开连接, 断开所有连接
        Button[] IP按钮 = new Button[2];
        /// <summary>
        /// 利用Button内的Tag确定(断开)连接的类型
        /// </summary>
        /// <param name="sender">点击的Button种类的</param>
        /// <param name="e"></param>
        private void 连接(object sender, EventArgs e)
        {
            ////清除文本信息
            //textBlock.Inlines.Clear();

            //grid.Children.Remove(IP按钮[0]);
            //grid.Children.Remove(IP按钮[1]);
            //确定学号和密码
            web.学生 = 学生信息[学号.SelectedItemId];
            //确定免费/收费地址
            short Tag;
            Button button = (Button)sender;
            Tag = short.Parse(button.Tag.ToString());
            //Convert.ToInt16(((Button)sender).Tag);//在主界面上点击按钮
            if (radioButton1.Checked && Tag == 0) Tag = 1;//收费

            string[] Content = web.连接(连接类型[Tag]);
            判断(Content);
        }

        private void 判断(string[] Content)
        {
            if (Content[0] == "错误")
            {
                textBlock.Text= Content[1];
                return;
            }
            if (Content[0].Contains("YES"))//(断开)连接成功, 显示信息
            {
                string 显示文本="";
                for (int i = 1; i < Content.Count(); i++)
                {
                    显示文本 = 显示文本 + Content[i] + "\n";
                    //if (连接Tag == 3) 图标.Icon = new System.Drawing.Icon(@"E:\Code\Visual Studio\网关\PC\Resources\2.ico");//收费连接更改图标
                    //else 图标.Icon = new System.Drawing.Icon(@"E:\Code\Visual Studio\网关\PC\Resources\1.ico");
                }
                textBlock.Text = 显示文本;
            }
            else//连接失败
            {
                断开指定连接(Content);
            }
        }
        private void 断开指定连接(string[] Content)
        {
            SetContentView(Resource.Layout.DisConnect);
            TextView textBlock;
            Button[] IP按钮 = new Button[2];

            textBlock = FindViewById<TextView>(Resource.Id.信息文本);
            IP按钮[0] = FindViewById<Button>(Resource.Id.button1);
            IP按钮[1] = FindViewById<Button>(Resource.Id.button2);

            int IP数量 = Content.Count() / 2;
            IP数量 = 2;
            string 显示文本 = "断开指定连接\n";
            for (int i = 0; i < IP数量; i++)
            {
                IP按钮[i].Visibility = ViewStates.Visible;//有IP地址就设为可见
                IP按钮[i].Tag = Content[2 * i];//增加Tag
                IP按钮[i].Click += (sender, e) =>
                {
                    设置初始页面();
                    string[] Content_断开连接 = web.断开指定连接(((Button)sender).Tag.ToString());
                    判断(Content_断开连接);
                };
                显示文本 += "IP" + (i + 1) + ":" + Content[2 * i] + "\n连接时间:" + Content[2 * i + 1] + "\n";
                textBlock.Text = 显示文本;
            }

        }
        private class f : AsyncTask<string, Boolean, string>
        {
            protected override string RunInBackground(params string[] @params)
            {
                throw new NotImplementedException();
            }
            protected override void OnPostExecute(string result)
            {
                base.OnPostExecute(result);
            }
        }
    }
}

